//
//  OMTNavigationBar.m
//  OMTUIKit
//
//  Created by 昭荣伊 on 2018/7/30.
//

#import "OMTNavigationBar.h"
#import <OMTCoreKit/OMTCoreKit.h>


// 默认标题大小
#define DEFAULT_TITLE_SIZE 17
// 默认按钮字体大小
#define DEFAULT_BUTTON_TITLE_FONT_SIZE 16
// 标题颜色
#define DEFAULT_TITLE_COLOR HexColor(0xCCCCCC)
// 背景色
#define DEFAULT_BACKGROUND_COLOR [UIColor whiteColor]

@interface OMTNavigationBar ()
@property (nonatomic, strong) UIView      *bottomLine;
@property (nonatomic, strong) UIView      *backgroundView;
@property (nonatomic, strong) UIImageView *backgroundImageView;

@end

@implementation OMTNavigationBar

@synthesize titleView = _titleView;

+ (instancetype)navigationBar {
    OMTNavigationBar *navigationBar = [[self alloc] initWithFrame:CGRectMake(0, 0, OMT_SCREEN_SIZE_WIDTH, [OMTNavigationBar navBarBottom])];
    return navigationBar;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
        [self setupConstraints];
    }
    return self;
}

- (void)setupSubviews {
    [self addSubview:self.backgroundView];
    [self addSubview:self.backgroundImageView];
    [self addSubview:self.leftButton];
    [self addSubview:self.rightButton];
    [self addSubview:self.bottomLine];
    [self addSubview:self.titleView];
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView.backgroundColor = DEFAULT_BACKGROUND_COLOR;
}

- (void)setupConstraints {
    NSInteger navigationBarHeight = 44;
    NSInteger top = IS_IPHONE_X ? 44 : 20;
    NSInteger margin = 16;
    NSInteger buttonHeight = 44;
    NSInteger buttonWidth = 44;
    
    CGSize leftButtonSize = [self.leftButton sizeThatFits:CGSizeMake(OMT_SCREEN_SIZE_WIDTH, MAXFLOAT)];
    CGSize rightButtonSize = [self.rightButton sizeThatFits:CGSizeMake(OMT_SCREEN_SIZE_WIDTH, MAXFLOAT)];
    NSInteger leftButtonWidth = leftButtonSize.width < 44 ? buttonWidth : leftButtonSize.width;
    NSInteger leftButtonHeight = leftButtonSize.height < 44 ? buttonHeight : leftButtonSize.height;
    NSInteger rightButtonWidth = rightButtonSize.width < 44 ? buttonWidth : rightButtonSize.width;
    NSInteger rightButtonHeight = rightButtonSize.height < 44 ? buttonHeight : rightButtonSize.height;
    
    CGSize titleSize = [self.titleView sizeThatFits:CGSizeMake(OMT_SCREEN_SIZE_WIDTH, MAXFLOAT)];
    NSInteger titleLabelHeight = titleSize.height > 0 ? MAX(self.titleView.bounds.size.height, titleSize.height) : 44;
    NSInteger titleLabelWidth = titleSize.width > 0 ? MAX(self.titleView.bounds.size.width, titleSize.width)  : 180;
    
    self.backgroundView.frame = self.bounds;
    self.backgroundImageView.frame = self.bounds;
    self.leftButton.width = leftButtonWidth;
    self.leftButton.height = leftButtonHeight;
    self.leftButton.top = top + (navigationBarHeight - leftButtonHeight) / 2;
    
    self.rightButton.width = rightButtonWidth;
    self.rightButton.height = rightButtonHeight;
    self.rightButton.top = top + (navigationBarHeight - rightButtonHeight) / 2;
    
    if ([UIApplication sharedApplication].omt_rightToLeftLanguage) {
        self.leftButton.right = OMT_SCREEN_SIZE_WIDTH - margin;
        self.leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        self.rightButton.left = margin;
    } else {
        self.leftButton.left = margin;
        self.leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        self.rightButton.right = OMT_SCREEN_SIZE_WIDTH - margin;
    }
    
    self.titleView.frame = CGRectMake((OMT_SCREEN_SIZE_WIDTH - titleLabelWidth) / 2, top + (navigationBarHeight - titleLabelHeight) / 2, titleLabelWidth, titleLabelHeight);
    self.bottomLine.frame = CGRectMake(0, (CGFloat)(self.bounds.size.height - 0.5), OMT_SCREEN_SIZE_WIDTH, 0.5);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self setupConstraints];
}

#pragma mark - Public
- (void)omt_setBottomLineHidden:(BOOL)hidden {
    self.bottomLine.hidden = hidden;
}

- (void)omt_setBackgroundAlpha:(CGFloat)alpha {
    self.backgroundView.alpha = alpha;
    self.backgroundImageView.alpha = alpha;
    self.bottomLine.alpha = alpha;
}

- (void)omt_setTintColor:(UIColor *)color {
    [self.leftButton setTitleColor:color forState:UIControlStateNormal];
    [self.rightButton setTitleColor:color forState:UIControlStateNormal];
    if ([self.titleView isKindOfClass:[UILabel class]]) {
        UILabel *titleLabel = (UILabel *)self.titleView;
        [titleLabel setTextColor:color];
    }
}

#pragma mark - 左侧按钮

- (void)omt_setLeftButtonWithNormal:(UIImage *)normal highlightedImage:(UIImage *)highlighted title:(NSString *)title titleColor:(UIColor *)titleColor {
    self.leftButton.hidden = NO;
    [self.leftButton setImage:normal forState:UIControlStateNormal];
    [self.leftButton setImage:highlighted forState:UIControlStateHighlighted];
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    [self.leftButton setTitleColor:titleColor forState:UIControlStateNormal];
}

- (void)omt_setLeftButtonWithImage:(UIImage *)image {
    [self omt_setLeftButtonWithNormal:image highlightedImage:image title:nil titleColor:nil];
}

- (void)omt_setLeftButtonWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage {
    [self omt_setLeftButtonWithNormal:image highlightedImage:highlightedImage title:nil titleColor:nil];
}

- (void)omt_setLeftButtonWithTitle:(NSString *)title {
    [self omt_setLeftButtonWithNormal:nil highlightedImage:nil title:title titleColor:nil];
}

- (void)omt_setLeftButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor; {
    [self omt_setLeftButtonWithNormal:nil highlightedImage:nil title:title titleColor:titleColor];
}

#pragma mark - 右侧按钮
- (void)omt_setRightButtonWithNormal:(UIImage *)normal highlightedImage:(UIImage *)highlighted title:(NSString *)title titleColor:(UIColor *)titleColor {
    self.rightButton.hidden = NO;
    [self.rightButton setImage:normal forState:UIControlStateNormal];
    [self.rightButton setImage:highlighted forState:UIControlStateHighlighted];
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton setTitleColor:titleColor forState:UIControlStateNormal];
}

- (void)omt_setRightButtonWithImage:(UIImage *)image {
    [self omt_setRightButtonWithNormal:image highlightedImage:image title:nil titleColor:nil];
}

- (void)omt_setRightButtonWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage {
    [self omt_setRightButtonWithNormal:image highlightedImage:highlightedImage title:nil titleColor:nil];
}

- (void)omt_setRightButtonWithTitle:(NSString *)title {
    [self omt_setRightButtonWithNormal:nil highlightedImage:nil title:title titleColor:nil];
}

- (void)omt_setRightButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor {
    [self omt_setRightButtonWithNormal:nil highlightedImage:nil title:title titleColor:titleColor];
}

#pragma mark - Event
- (void)leftViewAction:(id)sender {
    if (self.clickLeftBlock) {
        self.clickLeftBlock(sender);
    }
}

- (void)rightViewAction:(id)sender {
    if (self.clickRightBlock) {
        self.clickRightBlock(sender);
    }
}


#pragma mark - Setters And Getters
- (void)setTitle:(NSString *)title {
    _title = title;
    if ([self.titleView isKindOfClass:[UILabel class]]) {
        UILabel *titleLabel = (UILabel *)self.titleView;
        titleLabel.hidden = NO;
        titleLabel.text = _title;
    }
}

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    if ([self.titleView isKindOfClass:[UILabel class]]) {
        UILabel *titleLabel = (UILabel *)self.titleView;
        titleLabel.textColor = titleColor;
    }
}

- (void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    if ([self.titleView isKindOfClass:[UILabel class]]) {
        UILabel *titleLabel = (UILabel *)self.titleView;
        titleLabel.font = _titleFont;
    }
}

- (void)setBarBackgroundColor:(UIColor *)barBackgroundColor {
    self.backgroundImageView.hidden = YES;
    _barBackgroundColor = barBackgroundColor;
    self.backgroundView.hidden = NO;
    self.backgroundView.backgroundColor = _barBackgroundColor;
}

- (void)setBarBackgroundImage:(UIImage *)barBackgroundImage {
    self.backgroundView.hidden = YES;
    _barBackgroundImage = barBackgroundImage;
    self.backgroundImageView.hidden = NO;
    self.backgroundImageView.image = _barBackgroundImage;
}

- (void)setTitleView:(UIView *)titleView {
    [_titleView removeFromSuperview];
    _titleView = titleView;
    [self addSubview:_titleView];
}

#pragma mark - lazyload
-(UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc] init];
        [_leftButton addTarget:self action:@selector(leftViewAction:) forControlEvents:UIControlEventTouchUpInside];
        _leftButton.imageView.contentMode = UIViewContentModeCenter;
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:DEFAULT_BUTTON_TITLE_FONT_SIZE];
        _leftButton.hidden = YES;
    }
    return _leftButton;
}
-(UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [[UIButton alloc] init];
        [_rightButton addTarget:self action:@selector(rightViewAction:) forControlEvents:UIControlEventTouchUpInside];
        _rightButton.imageView.contentMode = UIViewContentModeCenter;
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:DEFAULT_BUTTON_TITLE_FONT_SIZE];
        _rightButton.hidden = YES;
    }
    return _rightButton;
}

- (UIView *)titleView {
    if (!_titleView) {
        UILabel *titleLable = [[UILabel alloc] init];
        titleLable.textColor = DEFAULT_TITLE_COLOR;
        titleLable.font = [UIFont boldSystemFontOfSize:DEFAULT_TITLE_SIZE];
        titleLable.textAlignment = NSTextAlignmentCenter;
        titleLable.hidden = YES;
        _titleView = titleLable;
    }
    return _titleView;
}

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithRed:(CGFloat)(218.0/255.0) green:(CGFloat)(218.0/255.0) blue:(CGFloat)(218.0/255.0) alpha:1.0];
    }
    return _bottomLine;
}
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] init];
    }
    return _backgroundView;
}
-(UIImageView *)backgroundImageView {
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc] init];
        _backgroundImageView.hidden = YES;
    }
    return _backgroundImageView;
}

+ (int)navBarBottom {
    return OMT_NAVIGATIONBAR_HEIGHT;
}

@end
